/**
 * @ Author: luoqi
 * @ Create Time: 2024-03-01 16:51
 * @ Modified by: luoqi
 * @ Modified time: 2024-03-01 17:13
 * @ Description:
 */

#include "str_convert.h"

#define IS_HEX_CHAR(c)      (((c >= 48) && (c <= 57)) || ((c >= 65) && (c <= 70)) || ((c >= 97) && (c <= 102)))

int to_hex_xx(const char *str, uint8_t *hex)
{
    if(str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) {
        if(IS_HEX_CHAR(str[2])){
            if(str[2] <= 57){
                *hex = str[2] - 48;
            }else if(str[2] <= 70){
                *hex = str[2] - 65 + 0x0a;
            }else {
                *hex = str[2] - 97 + 0x0a;
            }
        }
        if(IS_HEX_CHAR(str[3])){
            (*hex) <<= 4;
            if(str[3] <= 57){
                *hex |= str[3] - 48;
            }else if(str[3] <= 70){
                *hex |= str[3] - 65 + 0x0a;
            }else {
                *hex |= str[3] - 97 + 0x0a;
            }
        }
        return 0;
    } else {
        return -1;
    }
}
